package com.rc.saas.mini.service.member;

import com.rc.saas.mini.common.constant.SocketConstant;
import com.rc.saas.mini.mapper.enterprise.EnterpriseMemberMapper;
import com.rc.saas.mini.mapper.member.MemberAccountMapper;
import com.rc.saas.mini.mapper.rc.RcMemberFlagMapper;
import com.rc.saas.mini.model.enterprise.EnterpriseMember;
import com.rc.saas.mini.model.member.MemberAccount;
import com.rc.saas.mini.model.member.MemberAccountExample;
import com.rc.saas.mini.model.rc.RcMemberFlag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @author wangzhengwei
 * @create 2021/12/18
 */
@Service
public class MemberAccountServiceImpl implements MemberAccountService {

    @Autowired
    private MemberAccountMapper memberAccountMapper;

    @Autowired
    private RcMemberFlagMapper rcMemberFlagMapper;

    @Autowired
    private EnterpriseMemberMapper enterpriseMemberMapper;

    @Override
    public MemberAccountMapper getMapper() {
        return memberAccountMapper;
    }

    @Override
    public MemberAccount selectByMemberId(String tenantCode,Long memberId) {
        MemberAccountExample example = new MemberAccountExample();
        example.createCriteria().andTenantCodeEqualTo(tenantCode).andMemberIdEqualTo(memberId);
        return memberAccountMapper.selectByExample(example).stream().findFirst().orElseGet(() -> null);
    }

    @Override
    public BigDecimal getAccountByChargeType(MemberAccount memberAccount,String accountType) {

        //支付的账户类型 0个人钱包 1企业钱包 2共享余额 3先付后退钱包 4.支付分钱包 默认0
        if ("1".equals(accountType)){
            RcMemberFlag rcMemberFlag = rcMemberFlagMapper.selectByPrimaryKey(memberAccount.getMemberId());
            EnterpriseMember enterpriseMember = enterpriseMemberMapper.selectByPrimaryKey(rcMemberFlag.getEnterpriseId());
            //企业余额加授信剩余额度
            BigDecimal money = enterpriseMember.getEnterpriseAccount().add(enterpriseMember.getRemainCreditAmount());
            return money;
        } else if ("0".equals(accountType)) {
            return memberAccount.getAccount();
        } else {
            return memberAccount.getPaymentRefundAccount();
        }
    }

}
